<?php

namespace App\Http\Controllers\User;

use App\Http\Requests\UserRequest;
use App\Http\Requests\UserUpRequest;
use App\Models\Article;
use App\Models\Role;
use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;

class UserController extends Controller
{
    public function index(){

    }

    public function create(){
        $roles = Role::where('active','0')->where('partid',Auth::user()->partid)->get();
        return view('akl.Admin.Admin_add',compact('roles'));
    }

    public function store(UserRequest $request){
        $user = Input::all();
        $user['photo'] = $request->get('imgs')[0];
        $user['password'] = Hash::make($request->all()['password']);
        $user['partid'] = Auth::user()->partid;
        $rel = User::create($user);
        if(!$rel) return back()->with('errors','添加失败');
        return back()->with('errors','添加成功');
    }

    public function edit($id){
        $user = User::where('id',$id)->where('partid',Auth::user()->partid)->first();
        $roles = Role::where('active','0')->where('partid',Auth::user()->partid)->get();
        if (empty($user)||empty($roles)) return redirect('/');
        return view('akl.Admin.Admin_edit',compact('user','roles'));
    }

    public function update(UserUpRequest $request){
        $rel = User::hasuser($request->all()['id']);
        if(!$rel) return back()->with('errors','无此用户');
        $rel = User::hasname($request->all()['id'],$request->all()['name']);
        if($rel) return back()->with('errors','用户名已存在');
        $rel = User::hasemail($request->all()['id'],$request->all()['email']);
        if($rel)return back()->with('errors','邮箱已存在');
        $input = array(
            "name" => $request->all()['name'],
            "email" => $request->all()['email'],
            "roles_id" => $request->all()['roles_id'],
            "introduce" => $request->all()['introduce'],
        );
        if( $img = $request->get('imgs')[0] ) $input['photo'] = $img;
        $rel = User::where('id',$request->all()['id'])->update($input);
        if(!$rel) return back()->with('errors','更新失败');
        return back()->with('errors','更新成功');
    }

    public function destroy($id){
        $rel = User::where('id',$id)->where('partid',Auth::user()->partid)->update(['active'=>'1']);
        if(!$rel) return back()->with('errors','删除失败，请重试');
        return back()->with('errors','删除成功');
    }

    public function showDesigner(){
        $role = Role::where('name',"设计师")->where('partid',session()->get('doain')->id)->first();
        $users = User::where('roles_id',$role['id'])->where('partid',session()->get('doain')->id)->paginate(10);
        $users = $this->addWater($users);
        return view('akl.Aikla.Team-index',compact('users','role'));
    }

    public function showPresenter(){
        $role = Role::where('name',"主持人")->where('partid',session()->get('doain')->id)->first();
        $users = User::where('roles_id',$role['id'])->where('partid',session()->get('doain')->id)->paginate(10);
        $users = $this->addWater($users);
        return view('akl.Aikla.Team-index',compact('users','role'));
    }

    public function showButler(){
        $role = Role::where('name',"婚礼管家")->where('partid',session()->get('doain')->id)->first();
        $users = User::where('roles_id',$role['id'])->where('partid',session()->get('doain')->id)->paginate(10);
        $users = $this->addWater($users);
        return view('akl.Aikla.Team-index',compact('users','role'));
    }

    public function showPhotographer(){
        $role = Role::where('name',"婚礼跟拍")->where('partid',session()->get('doain')->id)->first();
        $users = User::where('roles_id',$role['id'])->where('partid',session()->get('doain')->id)->paginate(10);
        $users = $this->addWater($users);
        return view('akl.Aikla.Team-index',compact('users','role'));
    }

    public function showMaquilleur(){
        $role = Role::where('name',"化妆师")->where('partid',session()->get('doain')->id)->first();
        $users = User::where('roles_id',$role['id'])->where('partid',session()->get('doain')->id)->paginate(10);
        $users = $this->addWater($users);
        return view('akl.Aikla.Team-index',compact('users','role'));
    }




    public function managerIndex(){
        $role = Role::where('name',"主持人")->where('partid',session()->get('doain')->id)->first();
        $users = User::where('roles_id',$role['id'])->where('partid',session()->get('doain')->id)->paginate(10);
        $users = $this->addWater($users);
        return view('akl.Aikla.Team-index',compact('users','role'));
    }

    public function teamDetail(){
        $id =  explode('/',url()->current())[7];
        $user = User::where('id',$id)->where('partid',session()->get('doain')->id)->first();
        $articles = Article::where('users_id',$user['id'])->where('partid',session()->get('doain')->id)->paginate(10);
        return view('akl.Aikla.Team-member',compact('user','articles'));
    }

    public function addWater($users){
        $water = base64_encode(session()->get('doain')->water);
        $last = "?watermark/2/text/".$water."/font/5a6L5L2T/fontsize/500/fill/#000000=/dissolve/50/gravity/SouthEast/dx/0/dy/0";
        for ( $i=0;$i<count($users);$i++ ){
            $users[$i]->photo = $users[$i]->photo.$last;
        }
        return $users;
    }


}
